<template>
	<div class="alertBox">
		<div class="alertBk"></div>
		<div class="wordBox">
			<div class="alertHead">
				<p class="tittle" v-if="showRoleTittle == 1">新建用户</p>
				<p class="tittle" v-else-if="showRoleTittle == 2">修改用户</p>
				<div class="myClose" @click="closeAlert">
					<img src="~_assets/close.png" class="myClose"/>
				</div>
			</div>
			<div class="alertBody">
				<div class="changeDiv">
					<p class="alertText"><sup>*</sup>手机号:</p>
					<input type="text" class="alertInput" placeholder="请输入手机号" maxlength="11" v-model="preferentialPhone"/>
				</div>
				<div class="changeDiv">
					<p class="alertText"><sup>*</sup>车牌号:</p>
					<input type="text" class="alertInput" placeholder="请输入车牌号" maxlength="8" v-model="preferentialCarNumber"/>
				</div>
				<div class="alertBut">
					<button class="isFalse" @click="closeAlert">取消</button>
					<button class="isTrue" @click="addRoleIsTrue">确认</button>
				</div>
			</div>
		</div>
	</div>
</template>

<script>
import {
  ADD_GROUPING_MEMBERS,
  UPDATE_BYID_GROUPING_MEMBERS
} from "@/config/url";
export default {
  props: ["items", "showRoleTittle", "roleItem", "rolesItem"],
  data() {
    return {
      preferentialPhone: this.rolesItem.preferentialPhone, //手机号
      preferentialCarNumber: this.rolesItem.preferentialCarNumber //车牌号
    };
  },
  methods: {
    closeAlert() {
      this.$emit("closeAlert");
    },
    addRoleIsTrue() {
      var phone = /^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/;
      var plateNumber = /^(([\\u4e00-\\u9fa5]{1}[A-Z]{1})[-]?|([wW][Jj][\\u4e00-\\u9fa5]{1}[-]?)|([a-zA-Z]{2}))([A-Za-z0-9]{5}|[DdFf][A-HJ-NP-Za-hj-np-z0-9][0-9]{4}|[0-9]{5}[DdFf])|[冀豫云辽黑湘皖鲁苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼渝京津沪新京军空海北沈兰济南广成使领A-Z]{1}[a-zA-Z0-9]{5}[a-zA-Z0-9挂学警港澳]{1}$/;
      //1是新增,2是修改
      if (this.showRoleTittle == 1) {
        if (this.preferentialPhone && this.preferentialCarNumber) {
          if (!phone.test(this.preferentialPhone)) {
            this.$message({
              message: "手机号格式不正确!",
              type: "error"
            });
            return;
          }
          if (!plateNumber.test(this.preferentialCarNumber)) {
            this.$message({
              message: "车牌号格式不正确!",
              type: "error"
            });
            return;
          }
          this.$post(ADD_GROUPING_MEMBERS, {
            preferentialPhone: this.preferentialPhone,
            preferentialCarNumber: this.preferentialCarNumber,
            groupingId: this.items.id
          }).then(res => {
              if(res.status == 1){
                this.$message({
                  showClose: true,
                  type: "success",
                  message: "添加成功!"
                });
                this.closeAlert();
                this.$parent.searchRoleList();
              }else{
                this.$message({
                  message: res.data,
                  type: 'warning'
                });
              }
            },
            err => {
              this.$message({
                showClose: true,
                type: "warning",
                message: "网络异常,请稍后再试!"
              });
            }
          );
        } else {
          this.$message({
            message: "必填项不能为空",
            type: "error"
          });
        }
      } else if (this.showRoleTittle == 2) {
        if (this.preferentialPhone && this.preferentialCarNumber) {
          if (!phone.test(this.preferentialPhone)) {
            this.$message({
              message: "手机号格式不正确!",
              type: "error"
            });
            return;
          }
          if (!plateNumber.test(this.preferentialCarNumber)) {
            this.$message({
              message: "车牌号格式不正确!",
              type: "error"
            });
            return;
          }
          this.$post(UPDATE_BYID_GROUPING_MEMBERS, {
            preferentialPhone: this.preferentialPhone,
            preferentialCarNumber: this.preferentialCarNumber,
            groupingId: this.items.id,
            id: this.roleItem.id
          }).then(
            res => {
              this.$message({
                showClose: true,
                type: "success",
                message: "修改成功!"
              });
              this.closeAlert();
              this.$parent.searchRoleList();
            },
            err => {
              this.$message({
                showClose: true,
                type: "warning",
                message: "网络异常,请稍后再试!"
              });
            }
          );
        } else {
          this.$message({
            message: "必填项不能为空",
            type: "error"
          });
        }
      }
    }
  }
};
</script>

<style>
</style>